package com.example.itemprocessor.skip;

import com.example.itemprocessor.retry.CustomRetryException;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Component;

/**
 * @author XieYa
 */
@Component
public class SkipItemProcess implements ItemProcessor<Object, Object> {

    private int attemptCount = 0;

    @Override
    public Object process(Object s) throws Exception {
        System.out.println("process item : " + s);
        if ("26".equalsIgnoreCase(s.toString())) {
            attemptCount++;
            if (attemptCount >= 3) {
                System.out.println("Retried " + attemptCount + " times success.");
                return String.valueOf(Integer.parseInt(s.toString()) * -1);
            } else {
                System.out.println("Processed the " + attemptCount + " times fail");
                throw new CustomRetryException("Process failed. Attempt : " + attemptCount);
            }
        } else {
            return String.valueOf(Integer.parseInt(s.toString()) * -1);
        }
    }
}
